<?php
/* SVN FILE: $Id: statistics_player.php 259 2010-06-18 12:11:13Z gregory.rome $ */
/**
 * Model.
 *
 * @todo Add file description.
 *
 * cBBweb : Digital Blood Bowl league interface (http://code.google.com/p/cbbweb/)
 * Copyright 2009-2010, Grégory Romé.
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2009-2010, Grégory Romé.
 * @link          http://code.google.com/p/cbbweb/ cBBweb Project
 * @package       cbbweb
 * @subpackage    cbbweb.models
 * @version       $Revision: 259 $
 * @modifiedby    $LastChangedBy: gregory.rome $
 * @lastmodified  $Date: 2010-06-18 12:11:13 +0000 (Fri, 18 Jun 2010) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Model.
 *
 * CREATE TABLE Statistics_Players (
 *   ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
 *   idPlayer_Listing INTEGER ,
 *   iMatchPlayed INTEGER ,
 *   iMVP INTEGER ,
 *   Inflicted_iPasses INTEGER ,
 *   Inflicted_iCatches INTEGER ,
 *   Inflicted_iInterceptions INTEGER ,
 *   Inflicted_iTouchdowns INTEGER ,
 *   Inflicted_iCasualties INTEGER ,
 *   Inflicted_iTackles INTEGER ,
 *   Inflicted_iKO INTEGER ,
 *   Inflicted_iStuns INTEGER ,
 *   Inflicted_iInjuries INTEGER ,
 *   Inflicted_iDead INTEGER ,
 *   Inflicted_iMetersRunning INTEGER ,
 *   Inflicted_iMetersPassing INTEGER ,
 *   Sustained_iInterceptions INTEGER ,
 *   Sustained_iCasualties INTEGER ,
 *   Sustained_iTackles INTEGER ,
 *   Sustained_iKO INTEGER ,
 *   Sustained_iStuns INTEGER ,
 *   Sustained_iInjuries INTEGER ,
 *   Sustained_iDead INTEGER )
 *
 * @todo Add model description.
 *
 * @package       cbbweb
 * @subpackage    cbbweb.models
 */
class StatisticsPlayer extends AppModel {

	var $name = 'StatisticsPlayer';
	var $primaryKey = 'ID';

	var $virtualFields = array(
		'Inflicted_fAvgPasses' => '(CAST(Inflicted_iPasses AS REAL) / iMatchPlayed)',
		'Inflicted_fAvgCatches' => '(CAST(Inflicted_iCatches AS REAL) / iMatchPlayed)',
		'Inflicted_fAvgInterceptions' => '(CAST(Inflicted_iInterceptions AS REAL) / iMatchPlayed)',
    	'Inflicted_fAvgTouchdowns' => '(CAST(CAST(Inflicted_iTouchdowns AS REAL) AS REAL) / iMatchPlayed)',
	  	'Inflicted_fAvgCasualties' => '(CAST(Inflicted_iCasualties AS REAL) / iMatchPlayed)',
		'Inflicted_fAvgTackles' => '(CAST(Inflicted_iTackles AS REAL) / iMatchPlayed)',
		'Inflicted_fAvgKO' => '(CAST(Inflicted_iKO AS REAL) / iMatchPlayed)',
		'Inflicted_fAvgStuns' => '(CAST(Inflicted_iStuns AS REAL) / iMatchPlayed)',
		'Inflicted_fAvgInjuries' => '(CAST(Inflicted_iInjuries AS REAL) / iMatchPlayed)',
		'Inflicted_fAvgDead' => '(CAST(Inflicted_iDead AS REAL) / iMatchPlayed)',
		'Inflicted_fAvgMetersRunning' => '(CAST(Inflicted_iMetersRunning AS REAL) / iMatchPlayed)',
		'Inflicted_fAvgMetersPassing' => '(CAST(Inflicted_iMetersRunning AS REAL) / iMatchPlayed)',
		'Sustained_fAvgInterceptions' => '(CAST(Sustained_iInterceptions AS REAL) / iMatchPlayed)',
		'Sustained_fAvgCasualties' => '(CAST(Sustained_iCasualties AS REAL) / iMatchPlayed)',
		'Sustained_fAvgTackles' => '(CAST(Sustained_iTackles AS REAL) / iMatchPlayed)',
		'Sustained_fAvgKO' =>  '(CAST(Sustained_iKO AS REAL) / iMatchPlayed)',
		'Sustained_fAvgStuns' => '(CAST(Sustained_iStuns AS REAL) / iMatchPlayed)',
		'Sustained_fAvgInjuries' => '(CAST(Sustained_iInjuries AS REAL) / iMatchPlayed)',
	);

	var $belongsTo = array(
		'Player' => array(
			'className' => 'Player',
			'foreignKey' => 'idPlayer_Listing',
			'fields' => array('Player.ID, Player.strName, Player.iNumber'),
		)
	);

	function afterFind($results, $primary=false) {
		if($primary == true) {
			if(Set::check($results, '0.0')) {
				foreach($results as $id => $result) {
					foreach ( $result[0] as $key => $value ) {
		   				$results[$id][$this->alias][$key] = $value;
					}
					unset($results[$id][0]);
				}

			}
	  }
		return $results;
	}

}
?>